package com.tiger.web.controller.communal;


import com.tiger.common.annotation.Log;
import com.tiger.common.core.controller.BaseController;
import com.tiger.common.core.domain.AjaxResult;
import com.tiger.common.core.domain.model.LoginUser;
import com.tiger.common.core.page.TableDataInfo;
import com.tiger.common.enums.BusinessType;
import com.tiger.common.utils.DateUtils;
import com.tiger.common.utils.ServletUtils;
import com.tiger.common.utils.spring.SpringUtils;
import com.tiger.communal.domain.Hydropower;
import com.tiger.communal.service.IHydropowerService;
import com.tiger.framework.web.service.TokenService;
import com.tiger.project.domain.BookValue;
import com.tiger.project.service.IBookValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 水/电总表Controller
 *
 * @author standard
 * @date 2022-10-13
 */
@Api(tags = "水/电总表")
@RestController
@RequestMapping("/system/hydropower")
public class HydropowerController extends BaseController {
    @Autowired
    private IHydropowerService hydropowerService;
    @Autowired
    private IBookValueService bookValueService;
    /**
     * 查询水/电总表列表
     */
    @ApiOperation(value = "查询水/电总表列表")
    //@PreAuthorize("@ss.hasPermi('system:hydropower:list')")
    @GetMapping("/list")
    public TableDataInfo list(Hydropower hydropower) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        hydropower.setCompanyId(loginUser.getUser().getDeptId());
        startPage();
        List<Hydropower> list = hydropowerService.selectHydropowerList(hydropower);
        return getDataTable(list);
    }


    /**
     * 获取水/电总表详细信息
     */
    @ApiOperation(value = "获取水/电总表列详细信息")
    //@PreAuthorize("@ss.hasPermi('system:hydropower:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(hydropowerService.selectHydropowerById(id));
    }

    /**
     * 新增水/电总表
     */
    @ApiOperation(value = "新增水/电总表")
    //@PreAuthorize("@ss.hasPermi('system:hydropower:add')")
//    @Log(title = "水/电总表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Hydropower hydropower) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        hydropower.setCompanyId(loginUser.getUser().getDeptId());
        hydropower.setIsDeleted(0L);
        hydropower.setCreatedAt(DateUtils.getNowDate());
        return toAjax(hydropowerService.insertHydropower(hydropower));
    }

    /**
     * 修改水/电总表
     */
    @ApiOperation(value = "修改水/电总表")
    //@PreAuthorize("@ss.hasPermi('system:hydropower:edit')")
//    @Log(title = "水/电总表", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Hydropower hydropower) {
        return toAjax(hydropowerService.updateHydropower(hydropower));
    }

    /**
     * 删除水/电总表
     */
    @ApiOperation(value = "删除水/电总表")
    //@PreAuthorize("@ss.hasPermi('system:hydropower:remove')")
//    @Log(title = "水/电总表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(hydropowerService.deleteHydropowerByIds(ids));
    }

    /**
     * 查询【账面价值】列表
     */
    @ApiOperation(value = "查询水电总表关联账面列表")
    //@PreAuthorize("@ss.hasPermi('system:value:list')")
    @GetMapping("/getBookValue")
    public TableDataInfo getBookValue(@ApiParam("总表id") Long id) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
//        startPage();
        List<BookValue> list = bookValueService.getBookValue(loginUser,id);
        return getDataTable(list);
    }

    /**
     * 查询水电费支出
     */
    @ApiOperation(value = "查询水电费支出")
    @PostMapping("/getTotalHydropowerAmount")
    public AjaxResult getTotalHydropowerAmount() {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(hydropowerService.getTotalHydropowerAmount(loginUser.getDeptId()));
    }
}
